package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MaterialAudit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.parent.SchoolActivitysDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

@ActivityInfo(layout = R.layout.activity_sh_exercise,title = "校外活动")
public class ShSchoolActivityActivity extends BaseActivity {
    @InjectParam
    int aId;
    @InjectParam
    int id;
    @InjectParam
    SchoolActivitysDetailEntity data;
    @BindView(R.id.tv_student_name)
    TextView tv_student_name;
    @BindView(R.id.tv_actName)
    TextView tv_actName;
    @BindView(R.id.tv_actAddress)
    TextView tv_actAddress;
    @BindView(R.id.tv_actLong)
    TextView tv_actLong;
    @BindView(R.id.tv_actTime)
    TextView tv_actTime;
    @BindView(R.id.tv_actType)
    TextView tv_actType;
    @BindView(R.id.edit_content)
    NumberEditText edit_content;
    @BindView(R.id.recyc_pic)
    RecyclerView recyc_pic;
    @BindView(R.id.tv_spr)
    TextView tv_spr;
    ChosePicAdapter adapter;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @BindView(R.id.layout_reson)
    View viewReson;
    @BindView(R.id.edit_reson)
    EditText editReson;
    @Override
    protected void init() {
        ShSchoolActivityActivity__JumpCenter.bind(this);
        tv_student_name.setText(data.getStudentName());
        tv_actName.setText(data.getTitle());
        tv_actAddress.setText(data.getAddress());
        tv_actTime.setText(data.getActivityTime());
        tv_actType.setText(data.getType());
        edit_content.setText(data.getSummary());
        tv_actLong.setText(data.getHours()+"");
        if (data.getImage()!= null && !data.getImage().isEmpty()){
            recyc_pic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
            adapter = new ChosePicAdapter(data.getImage(), this,false);
            recyc_pic.setAdapter(adapter);
        }
        tv_spr.setText(ChosePeopleActivity.getPeoplesInfos3(data.getAuditors()));
        switch (data.getState()){
            case MaterialAuditHistoryFragment.TYPE_NOT_LOOK:
                viewBotton.setVisibility(View.VISIBLE);

                break;
            case MaterialAuditHistoryFragment.TYPE_REFUSE:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
            case MaterialAuditHistoryFragment.TYPE_OK:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
        }
        editReson.setText(data.getRefute());
        viewBotton.setVisibility(data.getMyState() == MaterialAuditHistoryFragment.TYPE_NOT_LOOK?View.VISIBLE:View.GONE);
    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .materialReview(aId,id,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(ShSchoolActivityActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }
}
